<template>
	<view>
		<u-navbar title="评价晒单" :autoBack="true" placeholder>
		</u-navbar>
		<scroll-view :scroll-y="true" :style="{height: scrollViewHeight}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton avatarSize="48" avatarShape="square" style="margin-bottom: 32rpx;" :loading="loading" avatar rows="7" v-for="i in 8" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else>
				<view v-for="(review, index) in list" :key="review.id" style="margin-bottom: 16rpx;">
					<view class="shop-rate u-border-bottom">
						<u--image mode="aspectFill" radius="8rpx" :src="review.shopImage" width="88rpx"
							height="88rpx"></u--image>
						<text style="font-size: 28rpx;color: #333;padding: 0 16rpx;">商品评分</text>
						<u-rate :active-color="appColor" inactive-color="#b2b2b2" :count="5" v-model="review.score"></u-rate>
					</view>
					<view class="shop-review">
						<u--textarea border="none" v-model="review.content" placeholder="请输入评价内容" count></u--textarea>
					</view>
					<view class="shop-upload">
						<view class="shop-upload-title">添加图片(最多3张)</view>
						<u-upload :fileList="review.image" @afterRead="afterRead" @delete="deletePic" :name="index" multiple
							:maxCount="3"></u-upload>
					</view>
					<view class="shop-anonymity">
						<u-radio-group
						    v-model="review.isAnonymous"
						    placement="row">
							<u-radio :customStyle="{marginRight: '8px'}" name="匿名发布" :activeColor="appColor" label="匿名发布"></u-radio>
							<u-radio name="公开发布" :activeColor="appColor" label="公开发布"></u-radio>
						</u-radio-group>
					</view>
				</view>
			</template>
		</scroll-view>
		<view class="good-detail-bootom">
			<view style="margin: 0 32rpx;height: 100%;width: calc(100vw - 64rpx);display: flex;align-items: center;justify-content: space-between;">
				<view class="good-detail-bootom-r">
					<u-button @click="submit" :color="appColor" text="提交" shape="circle"></u-button>
				</view>
			</view>	
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import {base_url} from '@/utils/request.js';
	import {getToken} from '@/utils/auth.js';
	import {queryById, goodsComment} from '@/api/shop.js';
	export default {
		data() {
			return {
				loading: true,
				list: [],
				orderId: undefined
			}
		},
		computed: {
			...mapState(['appInfo']),
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 50 + 'px';
			}
		},
		onLoad({id}) {
			this.loading = true;
			queryById({id}).then(res => {
				let list = [];
				this.orderId = res.result.id;
				res.result.detailList.forEach(item => {
					const {goodsId, id, goodsImage} = item;
					list.push({goodsId, id, score: 5, content: '', image: [], isAnonymous: '匿名发布', shopImage: goodsImage[0].url});
				});
				this.list = list;
			}).finally(() => {
				this.loading = false;
			})
		},
		methods: {
			// 提交
			submit() {
				let commentList = [];
				this.list.forEach(item => {
					let {goodsId, score, content, image, isAnonymous} = item;
					let obj = {
						content: content ? content : '好评',
						goodsId,
						image,
						score,
						isAnonymous: isAnonymous === '匿名发布' ? 1 : 0
					};
					commentList.push(obj);
				})
				this.$cuLoading().then(() => {
					goodsComment(this.orderId, commentList).then(res => {
						uni.hideLoading();
						this.$cuTip('感谢评价').then(() => {
							uni.navigateBack({
								delta: 1
							})
						});
					}).catch(() => {
						uni.hideLoading();
					})
				})
			},
			// 删除图片
			deletePic(event) {
				let curList = this.list[event.name].image;
				curList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let curList = this.list[event.name].image;
				let fileListLen = curList.length
				lists.map((item) => {
					curList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = curList[fileListLen]
					curList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: base_url + '/jeecg-boot/sys/common/upload', // 仅为示例，非真实的接口地址
						header: {
							'content-type': 'application/json',
							'Login-Type': 'MEMBER',
							'X-Access-Token': getToken(),
						},
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(JSON.parse(res.data).message)
						}
					});
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>

<style lang="scss" scoped>
	.shop-rate {
		padding: 32rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
	}

	.shop-review {
		padding: 0 16rpx;
		background-color: #fff;
	}

	.shop-upload {
		padding: 32rpx;
		background-color: #fff;
		.shop-upload-title {
			font-size: 30rpx;
			color: #000;
			padding-bottom: 16rpx;
		}
	}
	.shop-anonymity {
		padding: 32rpx;
		padding-top: 0;
		background-color: #fff;
	}
	
	.good-detail-bootom {
		background-color: #ffffff;
		position: fixed;
		height: 50px;
		width: 100vw;
		bottom: 0;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);     /*兼容 iOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);  /*兼容 iOS>11.2*/
		box-sizing: content-box;
		.good-detail-bootom-l1 {
			display: flex;
			flex-direction: column;
			justify-content: center;
			.good-detail-bootom-l1-t {
				font-size: 28rpx;
				padding-bottom: 4rpx;
			}
			.good-detail-bootom-l1-b {
				.good-detail-bootom-l1-b-1 {
					display: flex;
					align-items: center;
					font-size: 24rpx;
				}
			}
		}
		.good-detail-bootom-r {
			flex: 1;
		}
	}
</style>
